#include "list.h"

// 封装函数实现双向循环链表的初始化--》初始化头结点(链表的第一个节点)
struct doublelist * list_init()
{
    // 定义指针申请堆空间
    struct doublelist * head = malloc(sizeof(struct doublelist));

    head->btn     = NULL;
    head->btn1    = NULL;
    head->img_dsc = NULL;
    head->next    = head;
    head->prev    = head;
    return head;
}

// 尾插
int list_insert_tail(char * name, double price, int stock, char * ver_name, lv_obj_t * btn,
                     const lv_img_dsc_t * img_dsc, struct doublelist * head)
{
    struct doublelist * p = head;
    // 找到尾部
    while(p->next != head) p = p->next;

    // 准备新的节点
    struct doublelist * newnode = malloc(sizeof(struct doublelist));

    strncpy(newnode->name, name, sizeof(newnode->name) - 1);
    newnode->name[sizeof(newnode->name) - 1] = '\0';

    newnode->price   = price;
    newnode->stock   = stock;
    newnode->btn     = btn;
    newnode->img_dsc = img_dsc;
    strncpy(newnode->ver_name, ver_name, sizeof(newnode->ver_name) - 1);
    newnode->ver_name[sizeof(newnode->ver_name) - 1] = '\0';

    newnode->prev = NULL;
    newnode->next = NULL;

    // 尾插这个新节点
    newnode->prev = p;
    p->next       = newnode;
    newnode->next = head;
    head->prev    = newnode;
    return 0;
}

int list_change(char * name, int stock, dlist * head)
{
    dlist * p = head;
    while(p != NULL) {
        p = p->next;
        if(strcmp(name, p->name) == 0) break;
    }
    p->stock += stock;
    return 0;
}

// 双向循环链表删除结点
int list_delete(char * name, struct doublelist * head)
{
    // 找到要删除的结点--》认为结点存在
    struct doublelist * p = head;
    while(p->next != head) {
        p = p->next;
        if(strcmp(name, p->name) == 0) break;
    }

    p->btn     = NULL;
    p->btn1    = NULL;
    p->img_dsc = NULL;

    // 所有的节点都是如下逻辑
    p->next->prev = p->prev;
    p->prev->next = p->next;
    p->prev       = NULL;
    p->next       = NULL;
    free(p);
    return 0;
}

// 双向循环链表查找结点
int list_search(char * name, struct doublelist * head)
{
    // 找到要查找的结点--》认为结点存在
    struct doublelist * p = head;
    while(p->next != head) {
        p = p->next;
        if(strcmp(name, p->name) == 0) break;
    }
    return 0;
}

// 打印链表
int list_pri(struct doublelist * head)
{
    struct doublelist * p = head;
    while(p->next != head) {
        p = p->next;
        printf("%s\n", p->name);
        printf("%f\n", p->price);
        printf("%d\n", p->stock);
        printf("%s\n", p->ver_name);
        printf("\n");
    }
    return 0;
}

int list_des(struct doublelist * head)
{
    if(head == NULL) // 处理空链表情况
        return 0;
    struct doublelist * p;
    while(head->next != head) {
        p = head->next;

        p->btn1    = NULL;
        p->btn     = NULL;
        p->img_dsc = NULL;

        head->next    = p->next;
        p->next->prev = head;

        p->next = NULL;
        p->prev = NULL;

        free(p);
    }
    free(head);
    return 0;
}

// 购物车链表
plist * plist_init()
{
    // 定义指针申请堆空间
    plist * head = malloc(sizeof(plist));

    head->btn  = NULL;
    head->next = head;
    head->prev = head;
    return head;
}

// 尾插
int plist_insert_tail(char * name, double price, int count, lv_obj_t * btn, plist * head)
{
    plist * p = head;
    // 找到尾部
    while(p->next != head) p = p->next;

    // 准备新的节点
    plist * newnode = malloc(sizeof(plist));

    strcpy(newnode->name, name);
    newnode->price    = price;
    newnode->count    = count;
    newnode->subtotal = price * count;

    newnode->btn  = NULL;
    newnode->prev = NULL;
    newnode->next = NULL;

    // 尾插这个新节点
    newnode->prev = p;
    p->next       = newnode;
    newnode->next = head;
    head->prev    = newnode;
    return 0;
}

int plist_delete(char * name, plist * head)
{
    // 找到要删除的结点--》认为结点存在
    plist * p = head;
    while(p->next != head) {
        p = p->next;
        if(strcmp(name, p->name) == 0) break;
    }

    p->btn = NULL;

    // 所有的节点都是如下逻辑
    p->next->prev = p->prev;
    p->prev->next = p->next;
    p->prev       = NULL;
    p->next       = NULL;
    free(p);
    return 0;
}

int plist_change(char * name, int count, plist * head)
{
    plist * p = head;
    while(p != NULL) {
        p = p->next;
        if(strcmp(name, p->name) == 0) break;
        if(p == head) return -1;
    }
    p->count += count;
    p->subtotal += p->price * count;
    return 0;
}

int plist_change1(char * name, int count, plist * head)
{
    plist * p = head;
    while(p != NULL) {
        p = p->next;
        if(strcmp(name, p->name) == 0) break;
        if(p == head) return -1;
    }
    p->count -= count;
    p->subtotal -= p->price * count;
    return 0;
}

int plist_search(char * name, plist * head)
{
    // 找到要查找的结点--》认为结点存在
    plist * p = head;
    while(p->next != head) {
        p = p->next;
        if(strcmp(name, p->name) == 0) break;
    }
    printf("%s\n", p->name);
    printf("%f\n", p->price);
    printf("%d\n", p->count);
    printf("%f\n", p->subtotal);
    printf("\n");
    return 0;
}

int plist_pri(plist * head)
{
    plist * p = head;
    while(p->next != head) {
        p = p->next;
        printf("%s\n", p->name);
        printf("%f\n", p->price);
        printf("%d\n", p->count);
        printf("%f\n", p->subtotal);
        printf("\n");
    }
    return 0;
}

int plist_des(plist * head)
{
    if(head == NULL) // 处理空链表情况
        return 0;
    plist * p;
    while(head->next != head) {
        p = head->next;

        p->count    = 0;
        p->price    = 0;
        p->subtotal = 0;

        p->btn = NULL;

        head->next    = p->next;
        p->next->prev = head;

        p->next = NULL;
        p->prev = NULL;

        free(p);
    }
    free(head);
    return 0;
}